package com.todoclipse.dao;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import com.todoclipse.api.Item;
import com.todoclipse.api.ProjectItem;
import com.todoclipse.common.Request;
import com.todoclipse.common.Request.KeyValue;

public class ItemDaoImpl implements ItemDao {
	private final Request request;
	private final String token;

	protected ItemDaoImpl(Request request, String token) {
		this.request = request;
		this.token = token;
	}

	
	public List<ProjectItem> getAllProjects() {
		JSONArray array = request.invoke("getProjects", new KeyValue("token",token));
		/*
		 * Get all projects A request is sent to getProjects:
		 * http://todoist.com/
		 * API/getProjects?token=fb5f22601ec566e48083213f7573e908a7a272e5 JSON
		 * data is returned: [{"user_id": 1, "name": "Test project", "color": 1,
		 * "collapsed": 0, "item_order": 1, "indent": 1, "cache_count": 4, "id":
		 * 22073}, {"user_id": 1, "name": "Another test project", "color": 2,
		 * "collapsed": 0, "item_order": 2, "indent": 1, "cache_count": 0, "id":
		 * 22074}, ...]
		 */
		List<ProjectItem> result=new ArrayList<ProjectItem>();
		for (int i = 0; i < array.size(); i++) {
			JSONObject object = (JSONObject)array.get(i);
			Long id = getLong(object,"id");
			Long userId = getLong(object, "user_id");
			String name = getString(object,"name");
			Color color = getColor(object,"color");
			Boolean collapsed = getBoolean(object, "collapsed");
			Integer itemOrder = getInteger(object, "item_order");
			Integer indent= getInteger(object, "indent");
			Integer cacheCount = getInteger(object, "cache_count");
//			printIndents(indent);

//			getCompletedItems(p);
//			getUncompletedItems(p);
			ProjectItem item=new ProjectItem(id,userId,name);
			item.setColor(color);
			item.setCollapsed(collapsed);
			item.setItemOrder(itemOrder);
			indent.intValue();//TODO
			item.setCacheCount(cacheCount);
			result.add(item);
		}
		Collections.sort(result,new Comparator<ProjectItem>(){
			public int compare(ProjectItem o1, ProjectItem o2) {
				return new Integer(o1.getItemOrder()).compareTo(o2.getItemOrder());
			}
		});
		return result;
	}

	private Color getColor(JSONObject object, String name) {
		return Color.decode(getString(object, name));
	}

	private int getInteger(JSONObject object, String name) {
		return getLong(object, name).intValue();
	}

	private String getString(JSONObject object, String name) {
		return (String) object.get(name);
	}

	private Long getLong(JSONObject object,String name) {
		return (Long)object.get(name);
	}

	private Boolean getBoolean(JSONObject object, String name) {
		long l = getLong(object, name);
		if(l==0) {
			return false;
		}else if(l==1) {
			return true;
		}else{
			return null;
		}
			
	}

	/* (non-Javadoc)
	 * @see com.todoclipse.dao.ItemDao#getProject(long)
	 */
	public ProjectItem getProject(long id) {
		/*
		 * Get project A request is sent to getProject with project_id:
		 * http://todoist.com/API/getProject?project_id=22073&token=
		 * fb5f22601ec566e48083213f7573e908a7a272e5 JSON data is returned:
		 * {"user_id": 1L, "name": "Test project", "color": 1L, "collapsed": 0L,
		 * "item_order": 1L, "indent": 1L, "cache_count": 4L, "id": 22073L}
		 */
		return null;
	}

	/* (non-Javadoc)
	 * @see com.todoclipse.dao.ItemDao#getAllLabels()
	 */
	public Set<String> getAllLabels() {
		/*
		 * Get all labels A request is sent to getLabels:
		 * http://todoist.com/API/
		 * getLabels?token=fb5f22601ec566e48083213f7573e908a7a272e5
		 */
		return null;
	}

	/* (non-Javadoc)
	 * @see com.todoclipse.dao.ItemDao#addProject(com.todoclipse.api.ProjectItem)
	 */
	public boolean addProject(ProjectItem p) {
		/*
		 * Add project A request is sent to addProject with these parameters
		 * name. Optional parameters are color, indent, order.
		 * https://todoist.com
		 * /API/addProject?token=fb5f22601ec566e48083213f7573e908a7a272e5
		 * ...&name=Aloha On success a JSON of the project is returned: {"name":
		 * "...", "user_id": ..., ...} or if not succeful, an error is returned:
		 * "ERROR_NAME_IS_EMPTY"
		 */
		return false;
	}

	/* (non-Javadoc)
	 * @see com.todoclipse.dao.ItemDao#updateProject(com.todoclipse.api.ProjectItem)
	 */
	public boolean updateProject(ProjectItem p) {
		/*
		 * Update project A request is sent to updateProject with these
		 * parameters proejct_id. Optional parameters are name, color, indent,
		 * order.https://todoist.com/API/updateProject?token=
		 * fb5f22601ec566e48083213f7573e908a7a272e5...&proejct_id=5&name=Narrrvi
		 * On success a JSON of the updated project is returned: {"name": "...",
		 * "user_id": ..., ...} or if not succeful, an error is returned:
		 * "ERROR_PROJECT_NOT_FOUND"
		 */
		return false;
	}

	/* (non-Javadoc)
	 * @see com.todoclipse.dao.ItemDao#deleteProject(com.todoclipse.api.ProjectItem)
	 */
	public boolean deleteProject(ProjectItem p) {
		/*
		 * Delete project A request is sent to deleteProject with these
		 * parameters proejct_id.https://todoist.com/API/deleteProject?token=
		 * fb5f22601ec566e48083213f7573e908a7a272e5...&proejct_id=6 On success
		 * "ok" is returned: "ok"
		 */
		return false;
	}

	/* (non-Javadoc)
	 * @see com.todoclipse.dao.ItemDao#getUncompletedItems(com.todoclipse.api.ProjectItem)
	 */
	public List<Item> getUncompletedItems(ProjectItem p) {
		/*
		 * Get uncompleted items A request is sent to getUncompletedItems with
		 * project_id:
		 * http://todoist.com/API/getUncompletedItems?project_id=22073
		 * &token=fb5f22601ec566e48083213f7573e908a7a272e5 JSON data is
		 * returned: [{"due_date": new Date("Sun Apr 29 23:59:59 2007"),
		 * "user_id": 1, "collapsed": 0, "in_history": 0, "priority": 1,
		 * "item_order": 1, "faded": 0, "content": "By these things", "indent":
		 * 1, "project_id": 22073, "id": 210870, "checked": 0, "date_string":
		 * "29. Apr 2007"}, {"due_date": null, "user_id": 1, "collapsed": 0,
		 * "in_history": 0, "priority": 1, "item_order": 2, "faded": 0,
		 * "content": "Milk", "indent": 2, "project_id": 22073, "id": 210867,
		 * "checked": 0, "date_string": ""}, ...]
		 */
		return null;
	}

	/* (non-Javadoc)
	 * @see com.todoclipse.dao.ItemDao#getCompletedItems(com.todoclipse.api.ProjectItem)
	 */
	public List<Item> getCompletedItems(ProjectItem p) {
		/*
		 * Get completed items A request is sent to getCompletedItems with
		 * project_id and optionally an offset:
		 * http://todoist.com/API/getCompletedItems
		 * ?project_id=22073&offset=0&token
		 * =fb5f22601ec566e48083213f7573e908a7a272e5 JSON data is returned:
		 * [{"due_date": null, "user_id": 1, "collapsed": 0, "in_history": 1,
		 * "priority": 1, "item_order": 2, "faded": 0, "content":
		 * "Fluffy ferret", "indent": 1, "project_id": 22073, "id": 210872,
		 * "checked": 1, "date_string": ""}, {"due_date": null, "user_id": 1,
		 * "collapsed": 0, "in_history": 1, "priority": 1, "item_order": 1,
		 * "faded": 0, "content": "Test", "indent": 1, "project_id": 22073,
		 * "id": 210871, "checked": 1, "date_string": ""} ...]
		 */
		return null;
	}

	/* (non-Javadoc)
	 * @see com.todoclipse.dao.ItemDao#addItemToAProject(com.todoclipse.api.ProjectItem)
	 */
	public Item addItemToAProject(ProjectItem p) {
		/*
		 * Add an item to a project A request is sent to addItem with
		 * project_id, content and optionally date_string, priority (int value
		 * from 1 to 4):
		 * http://todoist.com/API/addItem?content=Test&project_id=22073
		 * &priority=1&token=fb5f22601ec566e48083213f7573e908a7a272e5 JSON data
		 * is returned: {"due_date": null, "user_id": 1, "collapsed": 0,
		 * "in_history": 0, "priority": 1, "item_order": 5, "faded": 0,
		 * "content": "Test", "indent": 1, "project_id": 22073, "id": 210873,
		 * "checked": 0, "date_string": null}
		 */
		return null;
	}

	/* (non-Javadoc)
	 * @see com.todoclipse.dao.ItemDao#updateItem(com.todoclipse.api.Item)
	 */
	public Item updateItem(Item t) {
		/*
		 * Update item A request is sent to updateItem with id, content and
		 * optionally a date_string, priority (int value from 1 to 4):
		 * http://todoist.com/API/updateItem?id=210873&content=TestHello&token=
		 * fb5f22601ec566e48083213f7573e908a7a272e5 JSON data of the item is
		 * returned: {"due_date": null, "user_id": 1, "collapsed": 0,
		 * "in_history": 0, "priority": 1, "item_order": 5, "faded": 0,
		 * "content": "TestHello", "indent": 1, "project_id": 22073, "id":
		 * 210873, "checked": 0, "date_string": null}
		 */
		return null;
	}

	/* (non-Javadoc)
	 * @see com.todoclipse.dao.ItemDao#deleteItem(com.todoclipse.api.Item)
	 */
	public boolean deleteItem(Item t) {
		/*
		 * Delete an item A request is sent to deleteItems with items ids and
		 * project_id.
		 */
		return false;
	}

	/* (non-Javadoc)
	 * @see com.todoclipse.dao.ItemDao#moveItemsToTheHistory(java.util.Collection)
	 */
	public void moveItemsToTheHistory(Collection<Item> tasks) {
		/*
		 * Move items to the history A request is sent to completeItems with
		 * item ids. http://todoist.com/API/completeItems?ids=[210873,
		 * 210873]&token=fb5f22601ec566e48083213f7573e908a7a272e5
		 */
	}

	/* (non-Javadoc)
	 * @see com.todoclipse.dao.ItemDao#getItemsById(java.util.Collection)
	 */
	public List<Item> getItemsById(Collection<Long> ids) {
		/*
		 * Get items by id A request is sent to getItemsById with item ids.
		 * http://todoist.com/API/getItemsById?ids=[210873,
		 * 210873]&token=fb5f22601ec566e48083213f7573e908a7a272e5
		 */
		return null;
	}

	/* (non-Javadoc)
	 * @see com.todoclipse.dao.ItemDao#execute(com.todoclipse.dao.ItemDaoImpl.Query)
	 */
	public void execute(Query q) {
		/*
		 * Queries A request is sent to query with: queries: A JSON string list
		 * of queries. Date format is 2007-4-29T10:59. Following queries are
		 * supported: viewall, overdue, p1, p2, p3 Example:
		 * http://todoist.com/API
		 * /query?queries=["2007-4-29T10:13","overdue","p1"
		 * ,"p2"]&token=fb5f22601ec566e48083213f7573e908a7a272e5 JSON data is
		 * returned: [{"type": "date", "query": "2007-4-29T10:13", "data":
		 * [[...]]}, {"type": "overdue", "data": [...]}, ...],
		 */
	}

	static public interface Query {
	}
}
